<?php

namespace app\shops\controller;

use think\Session;
use think\Db;
use think\Model;

/**
 * 增值服务管理
 */
class Appreciate extends Base
{

    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize = input('limit') ? input('limit') : $conf['paginate']['list_rows'];

        //拿到全部营销方案
        $all_list = Db('appreciate')->where('status', 0)->where('is_del',0)->order("id desc")->paginate($pageSize);
        $all_list1 = $all_list->toArray();


        //拿到当前店铺全部营销方案
        $list = Db('appreciate_user')->where('shop_id',$this->shop_id)->where('status',0)->field('add_value_id')->select();

        foreach ($all_list1['data'] as $k=>$v){
            $all_list1['data'][$k]['flag']=0;
            foreach ($list as $key=>$val){
                if ($v['id'] == $val['add_value_id']){
                    $all_list1['data'][$k]['flag']=1;
                }
            }
        }


        $this->assign('total', $all_list->total());
        $this->assign('page', $all_list->render());
        $this->assign('all_list', $all_list1);
        $this->assign('list', $list);
        return $this->fetch();

    }

    /**
     * @throws
     * 用户购买方案
     * @return [type] [description]
     */

    public function appreciate_user()
    {

        if (request()->isPost()) {

            $add_value_id  = input('id');
            $id = Db('appreciate_user')->where('add_value_id',$add_value_id)->value('id');
                //到期时间
                $day = Db('appreciate')->where('id',$add_value_id)->value('period_validity');
                $data = [
                    'shop_id'      =>$this->shop_id,
                    'add_value_id' => $add_value_id,
                    'buy_time'     => time(),
                    'expiration_time'=>$day*60*60*24+time()
                ];
                $res = Db('appreciate_user')->insert($data);
                if ($res) {
                    $this->success('购买成功','',[$add_value_id]);
                } else {
                    $this->error('网络异常');
                }
            }

    }


}
